package yxy.game.pm2.active.command.startup;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_武将直升活动;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostGachaTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/****************************************
 * @name CMD_Active_武将直升_任务_DrawReward
 * @description 武将直升任务奖励领取
 * @author 梁健彬
 * @since 2022-07-7
 ***************************************/
@MessageType(proto = Command.ActStartupTaskDrawReward, classtype = ActiveMessage.ActStartupTaskDrawRewardRequest.class)
public class CMD_Active_武将直升_任务_DrawReward extends Command<ActiveMessage.ActStartupTaskDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActStartupTaskDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int id = request.getId();//任务的id
		ActiveMessage.ActStartupTaskDrawRewardResponse.Builder response = ActiveMessage.ActStartupTaskDrawRewardResponse.newBuilder();

		try {
			ActiveModule_武将直升活动 am = ActiveModule_武将直升活动.get(playercode,false);
			if(am == null) {
				send(messageSource, response, Result.ActNoOpen);//活动不存在
				return;
			}

			if(!am.running()) {
				send(messageSource, response, Result.ActNoOpen);//活动没开启
				return;
			}

			if(!am.getTaskMap().containsKey(id)) {
				send(messageSource, response, Result.DataError);//如果不包含该id，参数错误
				return;
			}

			GhostGachaTask gachaTask = am.getTaskMap().get(id);

			String taskcode = gachaTask.getTaskcode();//taskcode
			Task taskbean = Task.get(playercode, taskcode);
			if(taskbean == null) {
				send(messageSource, response, Result.DataError);//task不存在
				return;
			}

			if(taskbean.isReward()) {
				send(messageSource, response, Result.ActHadGet);
				return;
			}

			if(!taskbean.isFinish()) {
				send(messageSource, response, Result.ActCannotGet);
				return;
			}

			task taskdata = DataCenter.getData(gachaTask.getTaskid(), task.class);//task表对应的那个task
			if(taskdata == null) {
				send(messageSource, response, Result.DataError);
				return;
			}

			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(taskdata.getRewardtype(), taskdata.getRewardid(), taskdata.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);

			response.addAllCurs(items);//返回奖励信息
			taskbean.setReward(true);
			taskbean.set();

			new CurrencyHandle().addCurrency(playercode, CCReason.活动_武将直升, curs);

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, ActiveMessage.ActStartupTaskDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
